﻿using Infrastructure.DbUtility;
using Infrastructure.Utility;
using System;
using System.Data;
using System.Linq;

public partial class admin_subject : BasePage
{

    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            this.Index = 1;
            int[] arr = this.LoadGrade(1, Request.QueryString["w"] ?? string.Empty, 0);
            if (arr.Length == 1) this.Response.Redirect("chapter.aspx?sid=" + arr[0]);
            if (Request["data"] == "list") this.GetAllSubject();
        }
    }
    private void GetAllSubject()
    {
        string sql = "SELECT * FROM Subject (NOLOCK) WHERE status = 1";
        DbHelper db = new DbHelper();
        var table = db.ExecQueryEx(sql);

        var arr = table.AsEnumerable().Select(r => string.Format("{{\"sid\":{0},\"name\":\"{1}\"}}", r.Value("sid"), r.Value("name")));

        Response.Clear();
        Response.Write("{\"success\": true,\"data\":[" + string.Join(",", arr) + "]}");
        Response.End();
    }
    private int[] LoadGrade(int index, string keyword, int nid)
    {
        string sql = string.Empty, filter = string.Empty;
        if (nid > 0) filter = " AND sid IN(SELECT sid FROM GradeSubject (NOLOCK) WHERE gid = " + nid + ") ";
        if (!string.IsNullOrWhiteSpace(keyword)) filter += " AND [name] LIKE  '%' + {0} + '%'";
        if (filter.Length > 0) filter = filter.Substring(4);

        sql = "SELECT COUNT(*) count FROM Subject (NOLOCK) " + (filter == string.Empty ? string.Empty : " WHERE " + filter) + Environment.NewLine;

        if (index == 1) sql += "SELECT TOP " + PAGE_SIZE + " * FROM Subject (NOLOCK)" + (filter == string.Empty ? string.Empty : " WHERE " + filter);
        else sql += "SELECT TOP " + PAGE_SIZE + " * FROM Subject (NOLOCK) WHERE sid NOT IN (SELECT TOP " + ((index - 1) * PAGE_SIZE) + " sid FROM Subject (NOLOCK))" + (filter == string.Empty ? string.Empty : " AND " + filter); ;

        DbHelper db = new DbHelper();
        var ds = db.ExecuteQueryEx(sql, keyword);

        this.Index = index;
        int count = ds.Tables[0].Rows[0].Value<int>("count");
        this.TotalSize = count / PAGE_SIZE + (count % PAGE_SIZE == 0 ? 0 : 1);

        lstGrade.DataSource = ds.Tables[1];
        lstGrade.DataBind();

        txtName.Text = keyword;

        return ds.Tables[1].AsEnumerable().Where(r => r.Value<bool>("ismenu")).Select(r => r.Value<int>("sid")).ToArray();
    }
    protected void btnPost_Click(object sender, EventArgs e)
    {
        var keyword = txtName.Text.Trim();
        var nid = int.Parse("0" + txtGradeId.Value);
        this.Index = 1;
        this.LoadGrade(1, keyword, nid);
    }
    protected void btnNavigate_Click(object sender, EventArgs e)
    {
        string sindex = Request["txtGoTo"];
        int index = 0;
        if (!int.TryParse(sindex, out index) || index < 1) return;

        var keyword = txtName.Text.Trim();
        var nid = int.Parse("0" + txtGradeId.Value);
        this.Index = index;

        this.LoadGrade(index, keyword, nid);
    }
    protected void btnDel_Click(object sender, EventArgs e)
    {
        var val = Request["selector"];
        if (string.IsNullOrWhiteSpace(val)) return;

        string sql = "DELETE FROM Subject WHERE sid IN (" + val + ")";

        DbHelper db = new DbHelper();
        db.ExecuteUpdate(sql);

        var keyword = txtName.Text.Trim();
        var nid = int.Parse("0" + txtGradeId.Value);
        this.LoadGrade(this.Index, keyword, nid);
    }
}